package com.libing.web;

import com.github.pagehelper.PageInfo;
import com.libing.base.BaseController;
import com.libing.entity.Customer;
import com.libing.entity.CustomerAllInfo;
import com.libing.entity.CustomerLevel;
import com.libing.enums.ResultInfoEnum;
import com.libing.enums.StateEnum;
import com.libing.service.CustomerService;
import com.libing.web.vo.CustomerVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 客户 Controller
 * @Auther: LEE
 * @Date: Create in 2018/2/4 11:23
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CustomerService customerService;

    /**
     * 客户新增
     * @param customer
     * @return
     */
    @PostMapping(value = "/", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public StateEnum insert(Customer customer) {
        logger.debug("新增客户资料： " + customer.toString());
        customer.setCreateId(getLoginUser().getId());
        customer.setCreateName(getLoginUser().getName());
        StateEnum result = customerService.insert(customer);
        return result;
    }

    /**
     * 更新客户
     * @param customer
     * @return
     */
    @PutMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
    public String update(@PathVariable("id") Long id, Customer customer, Model model) {
        logger.debug("修改客户信息ID:" + id + "，信息： " + customer.toString());
        if (!customer.getId().equals(id)) {
            model.addAttribute("result", ResultInfoEnum.DISAGREE.getInfo());
        } else {
            StateEnum result = customerService.update(customer);
            model.addAttribute("result", result.getStateInfo());
        }
        return "redirect:/customer/list";
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
    public String delete(@PathVariable("id") Long id, Model model) {
        logger.debug("删除客户信息： id = " + id);
        StateEnum result = customerService.delete(id);
        model.addAttribute("result", result.getStateInfo());
        return "redirect:/customer/list";
    }

    @PostMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Customer select(@PathVariable("id") Long id) {
        logger.info("获取客户详细信息，ID=[{}]", id);
        return customerService.selectById(id);
    }

    @GetMapping(value = "/addCustomer", produces = {"application/json;charset=UTF-8"})
    public String addCustomer(Model model) {
        model.addAttribute("user", getLoginUser());
        return "customer-add";
    }

//    @PostMapping(value = "/insert", produces = {"application/json;charset=UTF-8"})
//    @ResponseBody
//    public Boolean insert(@RequestBody CustomerAllInfo customerAllInfo) {
//        logger.info("获取新增用户信息：[{}]", customerAllInfo);
//        return customerService.insert(customerAllInfo, getLoginUser());
//    }

    /**
     * 查询用户
     *
     * @param nameOrContact 检索关键字，姓名或联系方式
     * @param companyName 检索关键字，公司名称
     * @param pageNum 页码
     * @param model
     * @return
     */
    @GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public String selectByParams(String nameOrContact, String companyName, Long levelId, @RequestParam(required = false, defaultValue = "1") Integer pageNum, Model model) {
        logger.debug("查询客户列表： nameOrContact = " + nameOrContact + ", companyName = " + companyName + ", levelId=" + levelId + ", pageNum = " + pageNum);
        PageInfo<CustomerVO> pageInfo = customerService.queryCustomersByParams(pageNum, nameOrContact, companyName, levelId);
        model.addAttribute("nameOrContact", nameOrContact);
        model.addAttribute("companyName", companyName);
        model.addAttribute("levelId", levelId);
        model.addAttribute("page", pageInfo);
        model.addAttribute("user", getLoginUser());
        return "customer-list";
    }

    @PostMapping(value = "/cusLevel/", produces = {"application/json;charest=UTF-8"})
    @ResponseBody
    public StateEnum insertCusLevel(CustomerLevel customerLevel) {
        logger.info("添加客户级别 ====>" + customerLevel.toString());
        return customerService.insertCusLevel(customerLevel);
    }

    @RequestMapping(value = "/updateCusLevel", method = RequestMethod.POST)
    @ResponseBody
    public StateEnum updateCusLevel(CustomerLevel customerLevel) {
        logger.info("修改客户级别 ===>" + customerLevel.toString());
        return customerService.updateCusLevel(customerLevel);
    }

    /**
     * 获取所有客户级别（下拉框显示）
     * @return
     */
    @RequestMapping(value = "/selCusLevelList", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<CustomerLevel> selCusLevelList() {
        return customerService.queryCustomerLevel(null);
    }

    /**
     * 获取用户级别列表
     * @param model
     * @return
     */
    @GetMapping(value = "/cusLevelList", produces = {"application/json;charset=UTF-8"})
    public String cusLevelList(Model model) {
        logger.debug("查询客户等级列表 ====>");
        List<CustomerLevel> list = customerService.queryCustomerLevel(null);
        model.addAttribute("list", list);
        model.addAttribute("user", getLoginUser());
        return "customer-level";
    }

    /**
     * 删除客户级别信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteCusLevel", method = RequestMethod.POST)
    @ResponseBody
    public StateEnum deleteCusLevel(Integer id) {
        logger.info("删除客户级别ID ===>" + id);
        return customerService.deleteCusLevel(id);
    }

}